var express = require('express');
var router = express.Router();

const socket = require('../common/my-socket');
const zSocket = new socket();

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/login', function (req, res, next) {
  
});

router.ws('/conectWs', function (ws, req) {
  const params = req.query;
  console.log("连接成功！");

  ws.on('connet', function open(req) {
    // console.log(req);
    console.log('connected');
  });
  ws.on('close', function close() {
    console.log('disconnected');
    // console.log(params);
    zSocket.offline(params.name);
  });
  ws.on('message', function incoming(data) {
    console.log("客服端发来消息" + data);
    data = JSON.parse(data);
    console.log(data.methodname);
    switch (data.methodname) {
      case 'user_login':
        zSocket.login(params.user,ws);
        break;
      case 'get_uer_list':
        let res = JSON.stringify(zSocket.get_user_list());
        ws.send(res);
        break;
      case 'send_msg':
        zSocket.sendMsg(data);
    }

  });
});


module.exports = router;
